/***
 * 此util主要提供课程数据的筛选和处理功能
 */
import { Course } from '../entity/Course';
import Constants from "../constants/Constants"
import DateUtil from "../utils/DateUtil"
import CourseService from "../service/CourseService"
import CommonUtil from "./CommonUtil"

class CourseUtil{

  //过滤本周课程
  filter_course(courseList:Array<Course>,filterWeek:string):Array<Course>{
    let courseListNew:Array<Course> = [];
    for (const item of courseList) {
      let statu:boolean = CommonUtil.judgeWeek(item.courseWeek,filterWeek);
      if (statu) {
        courseListNew.push(item);
      }
    }
    return courseListNew;
  }

  //解析课程行号
  parseRow(row:string):number{
    let rowNew:number = 0;
    switch(row)
    {
      case "第1-2节": rowNew = 0;break;
      case "第3-4节": rowNew = 1;break;
      case "第5-6节": rowNew = 2;break;
      case "第7-8节": rowNew = 3;break;
      case "第9-10节": rowNew = 4;break;
      default:break;
    }
    return rowNew;
  }

  //解析课程列号
  parseColumn(coloumn:string):number{
    let columnNew:number = 0;
    switch(coloumn)
    {
      case "周一": columnNew = 0;break;
      case "周二": columnNew = 1;break;
      case "周三": columnNew = 2;break;
      case "周四": columnNew = 3;break;
      case "周五": columnNew = 4;break;
      case "周六": columnNew = 5;break;
      case "周日": columnNew = 6;break;
      default:break;
    }
    return columnNew;
  }

  //解析课程周数，将number列表转为boolean列表
  parseNumBollean(weekStr:string):boolean[]{
    let weeNumber:number = Constants.weekNumber;
    let weekBooleanList:boolean[] = [];
    for (let i = 0; i < weeNumber; i++) {
      let statu:boolean = false;
      for (const e of weekStr.split(",")) {
        if(Number(e)==(i+1)){
          statu = true;
          break;
        }else {
          statu = false;
        }
      }
      if (statu) {
        weekBooleanList.push(true);
      }else {
        weekBooleanList.push(false);
      }
    }
    return weekBooleanList;
  }

  //过滤服务卡片当天课程(用于服务卡片上的卡片展示)
  async filterFormWeekCourse(courseList:Array<Course>){
    courseList = await CourseService.queryCourseData();

    //当天服务卡片展示的课程列表
    let courseNewList:Array<Course> = [];
    //服务卡片可以展示的列表
    let courseFormList:Array<Course> = [];
    // Number类型周几
    let weekNumber:number = DateUtil.weekDay;
    // 字符串类型周几
    let weekStr:number = 0;

    console.log("filterFormWeekCourse-------:" + JSON.stringify(courseList));

    for (const item of courseList) {
      weekStr = DateUtil.parseWeekString_Number(item.courseTime.split(" ")[0]);
      item.courseTime = DateUtil.parseTimeString_Number(item.courseTime.split(" ")[1]);
      if(weekStr == weekNumber){
        courseNewList.push(item);
      }
    }

    console.log("courseNewList:-------" + JSON.stringify(courseNewList));

    for (const item of courseNewList) {
      // 现在时间
      let nowDate:Date = new Date();
      // 课程结束时间
      let courseOverTime:Date = new Date(`${nowDate.getMonth()} ${nowDate.getDate()},${nowDate.getFullYear()} ${nowDate.getHours()}:${nowDate.getMinutes()}:00`);
      if((courseOverTime > nowDate) && (courseFormList.length<=2)){
        courseFormList.push(item);
      }
    }
    console.log("courseFormList:-------" + JSON.stringify(courseFormList));

    return courseFormList;
  }

}

let courseUtil = new CourseUtil();
export default courseUtil as CourseUtil;